package com.ruoyi.web.controller.system;

import java.io.File;
import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysGame;
import com.ruoyi.system.service.ISysGameService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 游戏管理Controller
 * 
 * @author ruoyi
 * @date 2025-07-22
 */
@RestController
@RequestMapping("/system/game")
public class SysGameController extends BaseController
{
    @Autowired
    private ISysGameService sysGameService;

    /**
     * 查询游戏管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:game:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGame sysGame)
    {
        startPage();
        List<SysGame> list = sysGameService.selectSysGameList(sysGame);
        return getDataTable(list);
    }

    /**
     * 获得所有游戏列表，无分页
     */
    @PreAuthorize("@ss.hasPermi('system:game:query')")
    @GetMapping("/all")
    public AjaxResult getAllGames()
    {
        return success(sysGameService.selectAllGames());
    }

    /**
     * 导出游戏管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:game:export')")
    @Log(title = "游戏管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGame sysGame)
    {
        List<SysGame> list = sysGameService.selectSysGameList(sysGame);
        ExcelUtil<SysGame> util = new ExcelUtil<SysGame>(SysGame.class);
        util.exportExcel(response, list, "游戏管理数据");
    }

    /**
     * 获取游戏管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:game:query')")
    @GetMapping(value = "/{gameId}")
    public AjaxResult getInfo(@PathVariable("gameId") Long gameId)
    {
        return success(sysGameService.selectSysGameByGameId(gameId));
    }

    /**
     * 新增游戏管理
     */
    @PreAuthorize("@ss.hasPermi('system:game:add')")
    @Log(title = "游戏管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGame sysGame)
    {
        return toAjax(sysGameService.insertSysGame(sysGame));
    }

    /**
     * 修改游戏管理
     */
    @PreAuthorize("@ss.hasPermi('system:game:edit')")
    @Log(title = "游戏管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGame sysGame)
    {
        return toAjax(sysGameService.updateSysGame(sysGame));
    }

    /**
     * 删除游戏管理
     */
    @PreAuthorize("@ss.hasPermi('system:game:remove')")
    @Log(title = "游戏管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gameIds}")
    public AjaxResult remove(@PathVariable Long[] gameIds)
    {
        return toAjax(sysGameService.deleteSysGameByGameIds(gameIds));
    }

    @Anonymous
    @PostMapping("/uploadImage")
    public AjaxResult uploadGameImage(@RequestParam("file") MultipartFile file) throws Exception {
        if (!file.getContentType().equals("image/jpeg") && !file.getContentType().equals("image/png")) {
            return AjaxResult.error("只允许上传 JPG 或 PNG 格式");
        }

        if (file.getSize() > 200 * 1024) {
            return AjaxResult.error("图片不能大于 200KB");
        }

        String uploadPath = RuoYiConfig.getProfile() + "/upload/gameImages";
        String fileName = FileUploadUtils.upload(uploadPath, file, MimeTypeUtils.IMAGE_EXTENSION, true);

        return AjaxResult.success("上传成功").put("url", fileName);
    }

}
